package com.iqiyi.pps.epg.core.rpc.vod;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.query.TResponse;
import com.iqiyi.pps.epg.api.model.web.vod.*;
import com.iqiyi.pps.epg.api.server.web.vod.VodWebService;
import com.iqiyi.pps.epg.core.model.vod.VodEpisodeLog;
import com.iqiyi.pps.epg.core.model.vod.VodLog;
import com.iqiyi.pps.epg.core.model.vod.VodRecord;
import com.iqiyi.pps.epg.core.model.vod.Vodad;
import com.iqiyi.pps.epg.core.service.vod.IVodService;
import com.iqiyi.pps.epg.core.service.vod.IVodadService;
import com.iqiyi.pps.epg.core.utils.Constants;
import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import com.iqiyi.pps.epg.core.utils.WriteSystemLogUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;

/**
 * Created by karkarrotxu on 2017/5/8.
 */
public class VodWebServiceImpl implements VodWebService.Iface {
    public static Logger logger = LoggerFactory.getLogger(VodWebServiceImpl.class);
    public static final String DB_ERROR = "数据库异常";

    private IVodService getVodService() {
        return WriteSystemLogUtil.getInstance().getVodService();
    }

    private IVodadService getVodadService() {
        return WriteSystemLogUtil.getInstance().getVodadService();
    }

    private static Object lockAddData = new Object();

    @Override
    public long saveLog(TVodLog tEntity) throws TException {
        VodLog entity = new VodLog();
        entity.setChannelId(tEntity.getChannelId());
        entity.setUser(tEntity.getUser());
        entity.setDescription(tEntity.getDescr());
        entity.setType(tEntity.getType());
        getVodService().saveLog(entity);
        return entity.getId();
    }

    @Override
    public TVodLogPage searchLog(TPage page, TFilter filter) throws TException {
        IVodService service = getVodService();
        return service.findLogPage(page, filter);
    }

    @Override
    public long saveEpisodeLog(TVodEpisodeLog tEntity) throws TException {
        VodEpisodeLog entity = new VodEpisodeLog();
        entity.setProgramId(tEntity.getProgramId());
        entity.setUser(tEntity.getUser());
        entity.setDescription(tEntity.getDescr());
        entity.setType(tEntity.getType());
        getVodService().saveEpisodeLog(entity);
        return entity.getId();
    }

    @Override
    public TVodEpisodeLogPage searchEpisodeLog(TPage page, TFilter filter) throws TException {
        IVodService service = getVodService();
        return service.findEpisodeLogPage(page, filter);
    }

    @Override
    public TVodRecordPage searchRecord(TPage page, TFilter filter) throws TException {
        IVodService service = getVodService();
        return service.findRecordPage(page, filter);
    }

    @Override
    public TResponse addRecord(TVodRecord entity) throws TException {
        TResponse tResponse = new TResponse();
        IVodService service = getVodService();
        synchronized (lockAddData) {
            VodRecord vodRecord = new VodRecord();
            vodRecord.setStartTime(entity.getStartTime());
            vodRecord.setEndTime(entity.getEndTime());
            vodRecord.setProjectWorkers(entity.getProjectWorkers());
            vodRecord.setNotifyType(entity.getNotifyType());
            vodRecord.setOpUser(entity.getOpUser());
            vodRecord.setChannelId(entity.getChannelId());
            long recordId = service.addRecord(vodRecord);
            if (recordId > 0) {
                tResponse.setCode(Constants.CODE_SUC);
                tResponse.setJsonData(recordId+"");
            } else {
                tResponse.setCode(Constants.CODE_FAIL);
                tResponse.setMsg(DB_ERROR);
            }
        }
        return tResponse;
    }

    @Override
    public TResponse editRecord(TVodRecord entity) throws TException {
        TResponse tResponse = new TResponse();
        IVodService service = getVodService();
        synchronized (lockAddData) {
            VodRecord vodRecord = service.findRecordById(entity.getId());
            try {
                boolean timeChange = hasTimeChange(vodRecord, entity);
                vodRecord.setStartTime(entity.getStartTime());
                vodRecord.setEndTime(entity.getEndTime());
                vodRecord.setProjectWorkers(entity.getProjectWorkers());
                vodRecord.setNotifyType(entity.getNotifyType());
                service.editRecord(vodRecord,timeChange);
                tResponse.setCode(Constants.CODE_SUC);
            } catch (Exception e) {
                logger.error( "[editRecord]",e );
                tResponse.setCode(Constants.CODE_FAIL);
                tResponse.setMsg(DB_ERROR);
            }
        }
        return tResponse;
    }

    private boolean hasTimeChange(VodRecord vodRecord, TVodRecord entity) {
        return !(vodRecord.getStartTime() + "|" + vodRecord.getEndTime()).equals(entity.getStartTime() + "|" + entity.getEndTime());
    }

    @Override
    public TResponse delRecord(TFilter filter) throws TException {
        IVodService service = getVodService();
        TResponse tResponse = new TResponse();
        try {
            int executeNum = service.delRecord(filter);
            tResponse.setCode(Constants.CODE_SUC);
            tResponse.setJsonData(executeNum + "");
        } catch (Exception e) {
            logger.error( "[delRecord]",e );
            tResponse.setCode(Constants.CODE_FAIL);
            tResponse.setMsg(DB_ERROR);
        }
        return tResponse;
    }

    @Override
    public TResponse saveAd(TVodad entity) throws TException {
        TResponse tResponse = new TResponse();
        IVodadService service = getVodadService();
        Vodad vodad = new Vodad();
        vodad.setName(entity.getName());
        vodad.setType(entity.getType());
        vodad.setFileSize(entity.getFileSize());
        vodad.setFileType(entity.getFileType());
        vodad.setContentType(Integer.parseInt(entity.getContentType()));
        vodad.setAddress(entity.getAddress());
        vodad.setPicAddress(entity.getPicAddress());
        vodad.setOpUser(entity.getOpUser());
        vodad.setOpTime(new Timestamp(DateTimeUtil.parseLongFormat(entity.getOpTime())));
        vodad.setStatus(Vodad.STATUS_IS_NOT_DELETE);
        vodad.setDuration(StringUtils.isNotBlank(entity.getDuration()) ? Long.parseLong(entity.getDuration()) : 0);
        vodad.setRemark(entity.getRemark());
        service.add(vodad);
        long id = vodad.getId();
        if (id > 0) {
            tResponse.setCode(Constants.CODE_SUC);
            tResponse.setJsonData(id+"");
        } else {
            tResponse.setCode(Constants.CODE_FAIL);
            tResponse.setMsg(DB_ERROR);
        }
        return tResponse;
    }

    @Override
    public TVodadPage searchAd(TPage page, TFilter filter) throws TException {
        IVodadService service = getVodadService();
        return service.findVodadList(page, filter);
    }

    @Override
    public TResponse delAd(long id) throws TException {
        TResponse tResponse = new TResponse();
        IVodadService service = getVodadService();
        service.delByStatus(id);
        tResponse.setCode(Constants.CODE_SUC);
        tResponse.setJsonData(id+"");
        return tResponse;
    }
}
